﻿using System.Collections.Generic;
using Microsoft.VisualStudio.TemplateWizard;
using Soat.SoPrism.TemplateWizards.WizardWindows.ProjectDescription;

namespace Soat.SoPrism.TemplateWizards
{
    /// <summary>
    /// Cette classe représente un Wizard permettant de générer une solution ou projet template
    /// en proposant un Wizard permettant de saisir des informations supplémentaires
    /// </summary>
    public class ProjectWizard : CommonWizard
    {
        const int MIN_TIME_FOR_PROJECT_TO_RELEASE_FILE_LOCK = 700;

        #region RunStarted Method

        /// <summary>
        /// Runs custom wizard logic at the beginning of a template wizard run.
        /// </summary>
        /// <param name="automationObject">The automation object being used by the template wizard.</param>
        /// <param name="replacementsDictionary">The list of standard parameters to be replaced.</param>
        /// <param name="runKind">A Microsoft.VisualStudio.TemplateWizard.WizardRunKind indicating the type of wizard run.</param>
        /// <param name="customParams">The custom parameters with which to perform parameter replacement in the project.</param>
        public override void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            base.RunStarted(automationObject,replacementsDictionary,runKind,customParams);

            // Création d'une instance de vue
            var view = new ProjectDescriptionView();
            var viewModel = new ProjectDescriptionViewModel(view);

            // Initialisation du ViewModel
            viewModel.Title = this.WizardParameterHelper.GetParameterValue("$title$",false);
            viewModel.CompanyName = this.WizardParameterHelper.GetParameterValue("$companyname$");
            viewModel.ProjectName = this.WizardParameterHelper.GetParameterValue("$projectshortname$");

            // Affichage de la vue permettant de sélectionner le nom du projet et de la société
            view.ShowDialog();

            // Enregistrement dans la Session Visual Studio
            this.WizardParameterHelper.SetParameterValue("$companyname$", viewModel.CompanyName);
            this.WizardParameterHelper.SetParameterValue("$projectshortname$",viewModel.ProjectName);

            // Effectue le remplacement dans les parametres a remplacer
            replacementsDictionary["$projectname$"] = viewModel.CompanyName + "." + viewModel.ProjectName;
            replacementsDictionary["$safeprojectname$"] = viewModel.CompanyName + "." + viewModel.ProjectName;
            replacementsDictionary["$companyname$"] =viewModel.CompanyName;
            replacementsDictionary["$projectshortname$"] = viewModel.ProjectName;

            this.WizardParameterHelper.SetParameterValue("$projectname$", replacementsDictionary["$projectname$"]);
            this.WizardParameterHelper.SetParameterValue("$safeprojectname$", replacementsDictionary["$safeprojectname$"]);
            this.WizardParameterHelper.SetParameterValue("$companyname$", replacementsDictionary["$companyname$"]);
            this.WizardParameterHelper.SetParameterValue("$projectshortname$", replacementsDictionary["$projectshortname$"]);

            viewModel.Dispose();

            System.Threading.Thread.Sleep(MIN_TIME_FOR_PROJECT_TO_RELEASE_FILE_LOCK);
        }

        #endregion
    }
}
